page.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. 'use client';
  2. import { use, useEffect, useRef } from 'react';
  3. import { useDonationHub } from '@/hooks/useDonationHub';
  4. import './style.scss';
  5. type Props = {
  6. params: Promise<{ widgetToken: string }>;
  7. };
  8. // 기본 스타일 설정 (TODO: API에서 DonationGoalConfig 조회)
  9. const DEFAULT_CONFIG = {
  10. barColor: '#FF6B35',
  11. barBackgroundColor: '#333333',
  12. barHeightPx: 30,
  13. titleFontSizePx: 18,
  14. titleFontColor: '#FFFFFF',
  15. amountFontSizePx: 14,
  16. amountFontColor: '#CCCCCC',
  17. isShowPercent: true
  18. };
  19. export default function GoalPage({ params }: Props) {
  20. const { widgetToken } = use(params);
  21. const hubUrl = process.env.NEXT_PUBLIC_API_URL + '/hubs/donation';
  22. const { goalProgress } = useDonationHub(widgetToken, hubUrl);
  23. const widgetRef = useRef<HTMLDivElement>(null);
  24. const cfg = DEFAULT_CONFIG;
  25. useEffect(() => {
  26. if (!widgetRef.current || !goalProgress) return;
  27. const el = widgetRef.current;
  28. el.style.setProperty('--goal-title-font-size', `${cfg.titleFontSizePx}px`);
  29. el.style.setProperty('--goal-title-color', cfg.titleFontColor);
  30. el.style.setProperty('--goal-bar-height', `${cfg.barHeightPx}px`);
  31. el.style.setProperty('--goal-bar-bg-color', cfg.barBackgroundColor);
  32. el.style.setProperty('--goal-bar-color', cfg.barColor);
  33. el.style.setProperty('--goal-bar-fill-width', `${Math.min(goalProgress.percent, 100)}%`);
  34. el.style.setProperty('--goal-amount-font-size', `${cfg.amountFontSizePx}px`);
  35. el.style.setProperty('--goal-amount-color', cfg.amountFontColor);
  36. }, [goalProgress, cfg]);
  37. if (!goalProgress) {
  38. return <div className="goal-widget goal-widget--loading">목표 데이터 대기 중...</div>;
  39. }
  40. const { title, currentAmount, targetAmount, percent } = goalProgress;
  41. return (
  42. <div ref={widgetRef} className="goal-widget">
  43. <div className="goal-title">
  44. {title}
  45. </div>
  46. <div className="goal-bar-wrapper">
  47. <div className="goal-bar-bg">
  48. <div className="goal-bar-fill" />
  49. </div>
  50. <div className="goal-bar-text">
  51. {currentAmount.toLocaleString()}원 / {targetAmount.toLocaleString()}원
  52. {cfg.isShowPercent && ` (${percent}%)`}
  53. </div>
  54. </div>
  55. </div>
  56. );
  57. }